package br.com.games.pyx.singleton;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileSet;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXParseException;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.sprite.TiledSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.opengl.texture.source.AssetTextureSource;
import org.anddev.andengine.opengl.texture.source.decorator.ColorKeyTextureSourceDecorator;
import org.anddev.andengine.opengl.texture.source.decorator.shape.RectangleTextureSourceDecoratorShape;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.SAXUtils;

import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import android.graphics.Color;
import br.com.games.pyx.atores.game.Ix;
import br.com.games.pyx.atores.game.Toupety;

public class FabricaRecursos {

	private static FabricaRecursos inst = new FabricaRecursos();
	private BaseGameActivity mainContext;
	private TextureRegion screenControlTextureRegion;
	private TextureRegion screenControlTextureknobRegion;
	private Texture screenControlTexture;
	private Texture screenControlKnobTexture;
	private Camera camera;
	private Engine engine;
	
	private FabricaRecursos() {
	}
	
	public static FabricaRecursos get() {
		return inst;
	}
	
	public void setCamera(Camera camera) {
		this.camera = camera;
	}
	
	public Camera getCamera() {
		return camera;
	}
	
	public Engine getEngine() {
		return engine;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	public BaseGameActivity getMainContext() {
		return mainContext;
	}
	
	public void setMainContext(BaseGameActivity mainContext) {
		this.mainContext = mainContext;
	}
	
	public TiledTextureRegion getTiledTextureRegion(String pAssetPath, final int pTexturePositionX, final int pTexturePositionY,
			final int pTileColumns, final int pTileRows, boolean flip, int textW, int textH) {
		
		Texture mTexture = new Texture(textW, textH, TextureOptions.BILINEAR_PREMULTIPLYALPHA);		
		
		TiledTextureRegion tiledRegion = TextureRegionFactory.createTiledFromAsset(mTexture, mainContext, pAssetPath, pTexturePositionX, pTexturePositionY, pTileColumns, pTileRows);
		tiledRegion.setFlippedHorizontal(true);
		
		mainContext.getEngine().getTextureManager().loadTexture(mTexture);
		
		return tiledRegion;
	}
	
	public Texture getTexture(String name) {
		
		final AssetTextureSource assetTextureSource = new AssetTextureSource(mainContext, name);
		
		Texture text = TextureFactory.createForTextureSourceSize(assetTextureSource, TextureOptions.DEFAULT);
		TextureRegionFactory.createFromSource(text, assetTextureSource, 0, 0);
		
		mainContext.getEngine().getTextureManager().loadTexture(text);
		
		return text;
	}
	
	
	public Sprite getSprite(TextureRegion region) {
		Sprite sp = new Sprite(0, 0, region);
		
		return sp;
	}
	
	
	public TiledSprite getTiledSprite(String pAssetPath, final int pTexturePositionX, final int pTexturePositionY,
			final int pTileColumns, final int pTileRows, boolean flip, int textW, int textH) {
		
		TiledSprite sp = new TiledSprite(0, 0, getTiledTextureRegion(pAssetPath, pTexturePositionX, pTexturePositionY, pTileColumns, pTileRows, flip, textW, textH));
		
		return sp;
	}
	
	public TextureRegion getTextureRegion(String image, int px, int py, int w, int h) {
		
		Texture text = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion textRegion = TextureRegionFactory.createFromAsset(text, mainContext, "imagens/"+image, px, py);
		
		mainContext.getEngine().getTextureManager().loadTexture(text);
		
		return textRegion;
	}	
	
	public AnimatedSprite getAnimatedSprite(TiledTextureRegion region, float x, float y, long frameDuration) {
		
		AnimatedSprite sprite = new AnimatedSprite(0, 0, region);
		sprite.animate(frameDuration);
		
		return sprite;
	}
	
	public TextureRegion getScreenControlTextureRegion() {
		
		if(screenControlTextureRegion == null) {
			this.screenControlTexture = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
			this.screenControlTextureRegion = TextureRegionFactory.createFromAsset(this.screenControlTexture, mainContext, "imagens/baseControle.png", 0, 0);
			
			mainContext.getEngine().getTextureManager().loadTexture(screenControlTexture);
		}
		
		return screenControlTextureRegion;
	}
	
	public TextureRegion getScreenControlKnobTextureRegion() {
		
		if(screenControlTextureknobRegion == null) {
			this.screenControlKnobTexture = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
			this.screenControlTextureknobRegion = TextureRegionFactory.createFromAsset(this.screenControlKnobTexture, mainContext, "imagens/knob.png", 0, 0);
			
			mainContext.getEngine().getTextureManager().loadTexture(screenControlKnobTexture);
		}
		
		return screenControlTextureknobRegion;
	}

	public Toupety getToupety() {
		Toupety toup = new Toupety();
		
		toup.init();
		
		return toup;
	}
	
	public Ix getIx() {
		Ix ator = new Ix();
		
		ator.init();
		
		return ator;
	}
	
}
